<template>
	<view>
		<view class="navbar" v-if="navList.length>0">
			<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{current: tabCurrentIndex === index}"
			 @click="tabClick(index,item)">
				{{item.text}}
			</view>
		</view>
		<navigator v-if="currentDeliveryType=='1'" url="/pages/address/address?source=1" class="address-section">
			<view class="order-content" v-if="addressData.name">
				<uni-icons type="location-filled" size="30"></uni-icons>
				<view class="cen">
					<view class="top">
						<text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.telephone}}</text>
					</view>
					<text class="address">{{addressData.province}}{{addressData.city}}{{addressData.area}}{{addressData.street}}</text>
				</view>
				<uni-icons type="right"></uni-icons>
			</view>
			<view class="order-content" v-if="!addressData.name">
				<uni-icons type="location-filled" size="30"></uni-icons>
				<view class="cen">
					<view class="top">
						<!-- <text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.telephone}}</text> -->
						<text>请选择地址</text>
					</view>
					<!-- <text class="address">{{addressData.street}}</text> -->
				</view>
				<uni-icons type="right"></uni-icons>
			</view>
			<image class="a-bg" src=""></image>
		</navigator>
		<view v-if="currentDeliveryType=='2'" class="delivery-section">
			<u-form :model="deliveryData">
				<u-form-item label="收件人姓名" label-width="250"><u-input type="text" v-model="deliveryData.deliveryName" placeholder="请输入姓名"/></u-form-item>
				<u-form-item label="收件人电话号码" label-width="250"><u-input type="number" v-model="deliveryData.deliveryContactNo" placeholder="请输入电话号码"/></u-form-item>
				<u-form-item label="收件地址" label-width="250" right-icon="arrow-right"><u-input readonly type="text" v-model="deliveryData.deliveryStreet"  placeholder="点击选择" @click="chooseLocation"/></u-form-item>
			</u-form>
		</view>
		<view v-if="currentDeliveryType=='3'" class="delivery-section">
			<u-form :model="deliveryData">
				<u-form-item label="提货人姓名" label-width="250"><u-input type="text" v-model="deliveryData.deliveryName" placeholder="请输入姓名"/></u-form-item>
				<u-form-item label="提货人电话号码" label-width="250"><u-input type="number" v-model="deliveryData.deliveryContactNo" placeholder="请输入电话号码"/></u-form-item>
			</u-form>
		</view>

		<view class="goods-section">
			<view class="store">
				<view class="m-name" v-if="product.merchantDTO">
				{{product.merchantDTO.merchantName}}
				</view>
				<view class="m-address" v-if="currentDeliveryType=='3'" @click="openLocation(product.merchantDTO.latitude, product.merchantDTO.longitude)">
					{{product.merchantDTO.province||''}}{{product.merchantDTO.city||''}}{{product.merchantDTO.district||''}}{{product.merchantDTO.merchantAddress||''}}
				</view>
			</view>
			<!-- 商品列表 -->
			<view class="g-item">
				<image  v-if="product.productMainImage" :src="product.productMainImage.url"></image>
				<view class="right">
					<text class="title clamp">{{product.productName}}</text>
					<text class="spec" v-if="product.skuEnabled">
						<text v-for="sku in productSku.skuAttrValueList">{{sku.skuAttrValue}}</text>
					</text>
					<text class="spec" v-if="!product.skuEnabled">-</text>
					<view class="price-box">
						<text class="price">￥{{seckill.unitPrice}}</text>
						<text class="number">x 1</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品金额</text>
				<text class="cell-tip red">￥{{productAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">优惠金额</text>
				<text class="cell-tip">￥{{deductAmount}}</text>
			</view>
			<view class="yt-list-cell b-b" v-if="currentDeliveryType=='1'">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip">￥{{freightAmount}}</text>
			</view>
			<view class="yt-list-cell b-b" v-if="currentDeliveryType=='2'">
				<text class="cell-tit clamp">配送费</text>
				<text class="cell-tip">￥{{deliveryAmount}}</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="memo" :placeholder="memoPlaceHolder" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{actualAmount}}</text>
			</view>
			<text class="submit" @click="submit">提交订单</text>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import uniPopup from "@/components/uni-popup/uni-popup.vue"
	export default {
		data() {
			return {
				tabCurrentIndex:0,
				currentDeliveryType:'',
				isDeliveryExpressEnabled:true,
				isDeliveryCityEnabled:true,
				isDeliveryPickEnabled:true,
				navList: [],
				quanlificationId: '',
				unit: 1,
				seckillId: '',
				seckill:{},
				productId: '',
				product:{},
				productSkuId: '',
				productSku: {},
				productAmount: 0.00, //商品金额, 未扣减任何优惠
				deductAmount: 0.00, //优惠金额
				actualAmount: 0.00, //实付金额
				freightAmount: 0.00, //运费
				addressData: {}, //收货地址
				deliveryData:{},	//配送地址
				deliveryAmount: 0.00, //配送费
				memo: '', //备注
				memoPlaceHolder: '请填写备注信息',
				type: ''
			}
		},
		components: {
			uniPopup
		},
		onLoad(option) {
			//商品数据
			this.quanlificationId = option.quanId;
			this.seckillId = option.seckillId;
			this.productId = option.productId;
			this.productSkuId = option.productSkuId;
			this.inquirySeckillById(this.seckillId);
			//全局支持的配送方式
			if(!this.applicationConfig.applicationDeliveryExpressEnabled){
				this.isDeliveryExpressEnabled = false;
			}
			if(!this.applicationConfig.applicationDeliveryCityEnabled){
				this.isDeliveryCityEnabled = false;
			}
			if(!this.applicationConfig.applicationDeliveryPickEnabled){
				this.isDeliveryPickEnabled = false;
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'applicationConfig'])
		},
		watch: {
			addressData(val) {
				this.calculateFreightAmount(this.seckillId, val.province, val.city, val.area);
			}
		},
		methods: {
			toActualAmount(productAmount,deductAmount,freightAmount){
				return (productAmount - deductAmount + freightAmount).toFixed(2);
			},
			tabClick(index,item) {
				this.tabCurrentIndex = index;
				this.currentDeliveryType = item.state;
				if(this.currentDeliveryType=='1'){
					this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,this.freightAmount);
				}else if(this.currentDeliveryType=='2'){
					this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,this.deliveryAmount);
				}else if(this.currentDeliveryType=='3'){
					this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,0.00);
				}
			},
			//查询默认收货地址
			inquiryDefaultAddress(userUuid) {
				let that = this;
				let postData = {
					userDTO: {
						userUuid: userUuid
					}
				}
				this.$api.request.userShip(postData, res => {
					if (res.body.status.statusCode === '0') {
						res.body.data.addresses.forEach(function(val, index) {
							if (val.default) {
								that.addressData = val;
							}
						})
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			//查询秒杀活动
			inquirySeckillById(seckillId) {
				let that = this;
				let postData = {
					secKillProductUuid: seckillId
				}
				this.$api.request.secKillDetail(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.seckill = res.body.data;
						this.product = res.body.data.productDTO;
						if(this.productSkuId){
							this.product.skuList.forEach(function(val,index){
								if(that.productSkuId === val.productSkuUuid){
									that.productSku = val;
								}
							})
						}
						that.productAmount = that.seckill.unitPrice.toFixed(2);
						this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,this.freightAmount);
						if(!that.product.deliveryExpressEnabled){
							that.isDeliveryExpressEnabled = false;
						}
						if(!that.product.deliveryCityEnabled){
							that.isDeliveryCityEnabled = false;
						}
						if(!that.product.deliveryPickEnabled){
							that.isDeliveryPickEnabled = false;
						}
						//商品支持的发货方式
						this.populateDeliveryType();
						
						//实物商品查询收货地址
						if(this.product.productType=='1')
							this.inquiryDefaultAddress(this.userInfo.userUuid);
						//虚拟商品需填写联系方式
						else if(this.product.productType=='2')
							this.memoPlaceHolder = '虚拟商品, 请务必填写手机号码以便商家联系!';
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			//打开商家地址
			openLocation(latitude,longitude){
				uni.openLocation({
					latitude: latitude,
					longitude: longitude,
					success: function () {
						console.log('打开地图成功');
					}
				});
			},
			//选择配送地址
			chooseLocation(){
				let that = this;
				uni.chooseLocation({
					success: function (res) {
						that.$set(that.deliveryData, 'deliveryStreet', res.address+res.name);
						that.$set(that.deliveryData, 'deliveryLatitude', res.latitude);
						that.$set(that.deliveryData, 'deliveryLongitude', res.longitude);
						that.calculateDeliveryAmount(that.seckillId, res.latitude, res.longitude);
					}
				});
			},
			//支持的发货方式
			populateDeliveryType(){
				if(this.isDeliveryExpressEnabled){
					this.navList.push({state:'1',text:'快递配送'});
				}
				if(this.isDeliveryCityEnabled){
					this.navList.push({state:'2',text:'同城配送'});
				}
				if(this.isDeliveryPickEnabled){
					this.navList.push({state:'3',text:'门店自提'});
				}
				if(this.navList.length>0)
					this.currentDeliveryType = this.navList[0].state;	//第一个为默认选择
			},
			// 计算运费金额
			calculateFreightAmount(seckillId, province, city, area) {
				let that = this;
				let postData = {
					secKillProduct:{
						secKillProductUuid: seckillId
					},
					orderDTO:{
						deliveryProvince: province,
						deliveryCity: city,
						deliveryArea: area,
					}
				}
				this.$api.request.calculateSecKillFreightAmount(postData, res => {
					if (res.body.status.statusCode === '0') {
						that.freightAmount = res.body.data.orderDTO.freightAmount;
						that.actualAmount = that.toActualAmount(that.productAmount,that.deductAmount,that.freightAmount);
					} else {
						that.$api.msg(res.body.status.errorDesc);
					}
				}, false);
			},
			// 计算同城配送费金额
			calculateDeliveryAmount(seckillId, deliveryLatitude, deliveryLongitude) {
				let that = this;
				let postData = {
					secKillProduct:{
						secKillProductUuid: seckillId
					},
					orderDTO:{
						deliveryLatitude: deliveryLatitude,
						deliveryLongitude: deliveryLongitude
					}	
				}
				this.$api.request.calculateSecKillDeliveryAmount(postData, res => {
					if (res.body.status.statusCode === '0') {
						that.deliveryAmount = res.body.data.orderDTO.deliveryAmount;
						//同城配送
						if(that.currentDeliveryType=='2')
							that.actualAmount = that.toActualAmount(that.productAmount,that.deductAmount,that.deliveryAmount);
					} else {
						that.$api.msg(res.body.status.errorDesc);
					}
				}, false);
			},
			change(e) {
				console.log('是否打开:' + e.show)
			},
			togglePopup(type, open) {
				switch (type) {
					case 'top':
						this.content = '顶部弹出 popup'
						break

					case 'bottom':
						this.content = '底部弹出 popup'
						break
					case 'center':
						this.content = '居中弹出 popup'
						break
				}
				this.type = type
				this.$nextTick(() => {
					this.$refs['show' + open].open()
				})
			},
			submit() {
				//检查收货地址是否选择
				if ((this.currentDeliveryType=='1')&&!this.addressData.userDeliveryAddressUuid) {
					this.$api.msg('未选择快递收货地址');
					return;
				}
				//检查配送地址是否选择
				if ((this.currentDeliveryType=='2')&&(!this.deliveryData.deliveryStreet||!this.deliveryData.deliveryName||!this.deliveryData.deliveryContactNo)) {
					this.$api.msg('配送地址未填写完整');
					return;
				}
				//检查提货人是否选择
				if ((this.currentDeliveryType=='3')&&(!this.deliveryData.deliveryName||!this.deliveryData.deliveryContactNo)) {
					this.$api.msg('提货人信息未填写完整');
					return;
				}
				//post订单数据到后台
				let postData = {
					secKillProduct: {
						secKillProductUuid: this.seckillId
					},
					orderDTO: {
						userDTO: {
							userUuid: this.userInfo.userUuid
						},
						deliveryAddressDTO: {
							userDeliveryAddressUuid: this.addressData.userDeliveryAddressUuid
						},
						deliveryName: this.deliveryData.deliveryName,
						deliveryContactNo: this.deliveryData.deliveryContactNo,
						deliveryLatitude: this.deliveryData.deliveryLatitude,
						deliveryLongitude: this.deliveryData.deliveryLongitude,
						deliveryStreet: this.deliveryData.deliveryStreet,
						deliveryType: this.currentDeliveryType,
						productUnit: this.unit,
						memo: this.memo
					},
					productDTO: {
						productUuid: this.productId
					},
					productSkuDTO: {
						productSkuUuid: this.productSkuId
					},
					quanlificationId: this.quanlificationId
				}
				this.$api.request.createSecKillOrder(postData, res => {
					if (res.body.status.statusCode === '0') {
						uni.redirectTo({
							url: '/pages/money/pay?orderNo=' + res.body.data.orderNo
						})
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				});

			},
			stopPrevent() {}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

		.icon-you {
			font-size: 32upx;
			color: $font-color-light;
			margin-right: 30upx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}
	.store{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		margin: 40upx 30upx 0 30upx;
		font-size: $font-sm;
		color: $font-color-dark;
		.m-name{
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			font-size: $font-lg;
			color: $font-color-dark;
		}
		.m-address{
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			max-width: 200px;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}
	.delivery-section{
		background-color: #fff;
		padding: 20upx 30upx 0 30upx;
	}
	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}

				.number {
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: $page-color-light;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: $uni-color-error;
			border-radius: 4upx;
			margin-right: 12upx;

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-light;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}

			& .close {
				color: $font-color-light;
				margin-left: 20upx;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;

		.price-content {
			padding-left: 30upx;
		}

		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}

		.price {
			font-size: 36upx;
			color: $base-color;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		/* position: relative; */
		z-index: 10;
	
		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;
	
			&.current {
				color: $base-color;
	
				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}
	}
</style>
